package openadmin.dao.operation;

import java.io.FileNotFoundException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

import openadmin.dao.Base;
import openadmin.dao.exception.DataException;
import openadmin.model.control.User;
import openadmin.model.person.Person;
import openadmin.util.configuration.TypeLanguages;
import openadmin.util.SerialClone;

public class TestDao {

	public static void main(String[] args) throws NoSuchAlgorithmException, FileNotFoundException, DataException {
					
		Date data = new Date();		
		
		String xx = "l'emili||l'Alex";		
		
		User usu = new User();		
		usu.setDescription("root");
		usu.setPassword("RootPassword");
		usu.setFullName("Alfred Oliver");
		usu.setIdentifier("234111");
		usu.setDateBegin(data);		
		usu.setLanguage(TypeLanguages.ca.toString());
		usu.setActive(true);
		
		//String s = "000000000" + usu.getIdentifier().trim();
		//String ss = s.substring(s.length() - 9, s.length());
		
		//System.out.println(ss);
		
		
		/**		
		User usu1 = new User();				
		usu1.setDescription("Kike");
		usu1.setPassword("Maite");
		usu1.setFullName("Alfred Oliver");
		usu1.setIdentifier("222222222");
		usu1.setDateBegin(data);
		usu1.setLanguage(TypeLanguages.ca.toString());	
		
		User usu2 = new User();				
		usu2.setDescription("Alex");
		usu2.setPassword("Maite");
		usu2.setFullName("Alfred Oliver");
		usu2.setIdentifier("11111111");
		usu2.setDateBegin(data);
		usu2.setLanguage(TypeLanguages.ca.toString());
		usu2.setDebugLog(false);
		*/
		
		Person per = new Person();
		
		//per.setDescription("Oliver Company Alfredo");
		//per.setHistoricLog(true);
		//per.setFullname("Oliver Company Alfredo");
		per.setDescription("%222222%");
		//per.setState('A');
		
		DaoOperationFacade clientLog = new DaoJpaHibernate(usu, "log_post", null);				
		LogOperationFacade log = new LogDao(clientLog, "persona");
		DaoOperationFacade client = new DaoJpaHibernate(usu, "ayto_post", log);		
		
			
		Person per2 = (Person)client.findObjectDescription(per);
		Person per1 = SerialClone.clone(per2);
		client.begin();
		
		if (per2 != null){
			per2.setNif("222222");
			per2.setName("alex");
			per2.setPartsurname1("del");
			per2.setSurname1("Oliver");
			per2.setSurname2("fort");
			per2.setNote("Prova");
			per2.setHistoricLog(true);
			log.detailLog(per1, per2);
			client.updateObject(per1, per2);
		}
		
		
		//client.persistObject(per);
		
		
		
		client.commit();
		
		/**
		//test persist
		client.begin();				
		
		if (client.findObjectDescription(usu) == null){
			client.persistObject(usu);
			System.out.println("Usu donat d'alta");	
		}		
		
		if (client.findObjectDescription(usu1) == null){
			client.persistObject(usu1);
			System.out.println("Usu_1 donat d'alta");	
		}	
		
		if (client.findObjectDescription(usu2) == null){
			client.persistObject(usu2);
			System.out.println("Usu_2 donat d'alta");	
		}	
		
		client.commit();
		
		if (client.isResultOperation()) System.out.println("Objecte donat d'alta");	
		
		//test update
		
		usu1 = (User)client.findObjectDescription(usu1);
		usu1.setDescription("xxxx");
		usu1.setLanguage(TypeLanguages.es.toString());
		usu1.setIdentifier("P4610700I");
		
		client.begin();				
		client.updateObject(usu1);
		client.commit();
		
		
		//test remove
		usu1 = (User)client.findObjectDescription(usu1);
		
		client.begin();				
		client.removeObject(usu1);
		client.commit();
		
		
		
		client.begin();
		
		List <Base> li;
					
		li = client.findObjects(usu);
		System.out.println("Result cerca tots " + li.size());
							
		client.commit();
		*/
		client.finalize();
		log.finalizeLog();
		

	}

}
